package com.farm.template.utils;

import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;

import com.googlecode.aviator.AviatorEvaluator;

/**
 * 公式运算器
 * 
 * @author macpl
 *
 */
public class Formulas {

	public static String executeFormula(String formula, Map<String, Object> para) {
		Object result = (Object) AviatorEvaluator.execute(formula, para);
		String point = String.valueOf(result.toString());
		return point;
	}

	public static Map<String, Object> getTestParas() {
		Map<String, Object> env2 = new HashMap<String, Object>();
		env2.put("TEXT1", "text1");
		env2.put("TEXT2", "text2");
		env2.put("TEXT3", "text3");
		env2.put("TEXT4", "text4");
		env2.put("TEXT5", "text5");
		env2.put("NUM6", 23);
		Map<String, Object> env = new HashMap<String, Object>();
		env.put("NUM1", 1.1);
		env.put("NUM2", 2);
		env.put("NUM3", 3.3);
		env.put("NUM4", 4);
		env.put("NUM5", 5.5);
		env.put("env2", env2);
		return env;
	}

	/**
	 * 获得测试参数描述
	 * 
	 * @return
	 */
	public static String getTestParasNote() {
		String note = "测试参数：";
		for (Entry<String, Object> node : getTestParas().entrySet()) {
			note = note + node.getKey() + "=" + node.getValue() + ";";
		}
		return note;
	}

	public static void main(String[] args) {
		System.out.println(executeFormula("env2.NUM8==1", getTestParas()));
	}
}
